package com.lsk.mobile;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsk.util.JsonUtil;
import com.lsk.util.Syslog;

import java.util.Map;

/**
 * 返利线程
 */
public class RebateThread implements Runnable {

    private Map map;
    public RebateThread(Map map){
        this.map=map;
    }
    @Override
    public void run() {
        String commodityId=map.get("commodityId")+"";//商品id
        String memberId=map.get("memberId")+"";//会员id
        String orgId=map.get("orgId")+"";//机构id
        String orderId=map.get("orderId")+"";
        /*执行返利*/
        /*查询机构返利规则*/
        double onePercentage=0.00;//一级返利比例
        double twoPercentage=0.00;//二级返利比例
        String commodityRuleSql="SELECT * from yx_rule  where commodityId='"+commodityId+"' ORDER BY createTime desc ";
        Record commodityRule=Db.findFirst(commodityRuleSql);
        if (commodityRule!=null){//此条商品单独设置了返利规则
            onePercentage=commodityRule.getDouble("onePercentage");
            twoPercentage=commodityRule.getDouble("twoPercentage");
        }else {//商品无返利规则，使用门店返利规则
            String orgRuleSql="SELECT * from yx_rule  where orgId='"+orgId+"' ORDER BY createTime desc ";
            Record orgRule=Db.findFirst(orgRuleSql);
            onePercentage=orgRule.getDouble("onePercentage");
            twoPercentage=orgRule.getDouble("twoPercentage");
        }
        String orderSql="SELECT *from yx_order  where id="+orderId;
        Record order=Db.findFirst(orderSql);
        int money=order.getInt("money");//订单交易的金额
        int oneMoney= (int) (money*onePercentage);//一级返利金额
        int twoMoney=(int) (money*twoPercentage);//二级返利金额
        /*会员部分*/
        String memberSql="SELECT *from yx_member where id='"+memberId+"'";
        Record memberRecord=Db.findFirst(memberSql);
        promoteMemberGrade(orgId,memberRecord.getInt("grade"),
                memberRecord.getInt("buyMoney")+money);
        memberRecord.set("buyMoney",memberRecord.getInt("buyMoney")+money).set("integral",money/100);
        Db.update("yx_member","id",memberRecord);//修改会员信息
        //查询当前会员的上级会员
        String memberParentId=memberRecord.getStr("parentId");
        if ("".equals(memberParentId)==false && memberParentId!=null){//有上级会员
            memberSql="SELECT *from yx_member where id='"+memberParentId+"'";
            Record parentMember=Db.findFirst(memberSql);
            parentMember.set("rebateMoney",parentMember.getInt("rebateMoney")+oneMoney)
                    .set("oneSonMember",parentMember.getInt("oneSonMember")+1);
            Db.update("yx_member","id",parentMember);//修改会员信息
            /*二级父级*/
            String parentMemberParentId=parentMember.getStr("parentId");
            if ("".equals(memberParentId)==false && memberParentId!=null){//有上级会员
                memberSql="SELECT *from yx_member where id='"+parentMemberParentId+"'";
                Record parentParentMember=Db.findFirst(memberSql);
                parentParentMember.set("rebateMoney",parentMember.getInt("rebateMoney")+twoMoney)
                        .set("oneSonMember",parentMember.getInt("twoSonMember")+1);
                Db.update("yx_member","id",parentParentMember);//修改会员信息
            }else {
                Syslog.info("会员 "+ JsonUtil.getJsonStr(memberRecord)+" 无二级父级");
            }
        }else {
            Syslog.info("会员 "+ JsonUtil.getJsonStr(memberRecord)+" 无一级父级");
        }

    }

    /**
     * 会员要晋升的等级
     * @param orgId  机构号
     * @param grade  当前的等级
     * @return
     */
   public int promoteMemberGrade(String orgId,int grade,int buyMoney){
       String sql="SELECT *from yx_member_grade where orgId="+orgId;
       Record record=Db.findFirst(sql);
       int resultGrade=0;
       int grade1=record.getInt("grade1");
       int grade2=record.getInt("grade2");
       int grade3=record.getInt("grade3");
       int grade4=record.getInt("grade4");
       int grade5=record.getInt("grade5");
       if (grade==0){//如果是游客
           if(buyMoney>grade1){//有购买即为普通会员 1
               resultGrade=1;
           }
       }
       if (grade==1){//如果是普通会员 1
           if(buyMoney>=grade2){//满足条件即为  2 白银会员
               resultGrade=2;
           }
       }
       if (grade==2){//如果是2 白银会员
           if(buyMoney>=grade3){//满足条件即为  3 黄金会员
               resultGrade=3;
           }
       }
       if (grade==3){//如果是3 黄金会员
           if(buyMoney>=grade4){//满足条件即为  4钻石会员
               resultGrade=4;
           }
       }
       if (grade==4){//如果是4钻石会员
           if(buyMoney>=grade5){//满足条件即为  5 至尊会员
               resultGrade=5;
           }
       }
       return resultGrade;
   }
}
